using System;
using Gtk;

namespace TracNghiem2
{
	public partial class question_player : Gtk.Window
	{
//					Pango.Layout layout;
//			Gtk.DrawingArea da;
		public question_player () : 
				base(Gtk.WindowType.Toplevel)
		{
			questions = new DeThi ();
			this.Build ();
			tv_question.WrapMode = WrapMode.Word;


			create_layout ();

//			da = new Gtk.DrawingArea();
//			int width = 20;
//			int height = 20;
//			rbtn_a.GetSizeRequest(out width, out height);
//			da.SetSizeRequest(width, height);
//
//			da.ExposeEvent += Expose_Event;
//
//			layout = new Pango.Layout(rbtn_a.PangoContext);
//			layout.Width = Pango.Units.FromPixels (width);
//			layout.Wrap = Pango.WrapMode.Word;
//			layout.Alignment = Pango.Alignment.Left;
//			layout.SetMarkup ("<span color=" + (char)34 + "blue" + (char)34 + ">" + "file by me" + "</span>");

//			rbtn_a.Add (da);
		}

//		void Expose_Event (object obj, ExposeEventArgs args)
//		{
//			da.GdkWindow.DrawLayout (da.Style.TextGC (StateType.Normal), 5, 5, layout);
//		}

		public DeThi questions;
		public int minute;
		public int seconds;
		public bool started;
		public int current_question;
		public string[] user_choices;
		fas fast;
		Pango.Layout layout_red;
		Pango.Layout layout_blue;
		Pango.Layout layout_default;
		public string answer_a;
		public string answer_b;
		public string answer_c;
		public string answer_d;
		public int wrong;
		public int right;
		public int score;
		public bool[] hint_enabled;

		void Expose_Event (object obj, ExposeEventArgs args)
		{

		}

		protected void OnLocate (object sender, EventArgs e)
		{
			FileChooserDialog open_file_dialog = new FileChooserDialog ("Open Questions Database",
			                                                           this,
			                                                           FileChooserAction.Open,
			                                                           "Cancel", ResponseType.Cancel, 
			                                                           "Open", ResponseType.Accept);
			if (open_file_dialog.Run () == (int)ResponseType.Accept) {
				if (questions.read_questions (open_file_dialog.Filename) == -1) { /* FIXME : I delete the old records for new records */
					/* FIXME : please add exceptions here */
				}
				
			}
			e_db.Text = open_file_dialog.Filename;
			user_choices = new string[questions.count ()];
			hint_enabled = new bool[questions.count ()];
			open_file_dialog.Destroy ();
		}

		protected void OnStart (object sender, EventArgs e)
		{ 
			current_question = 0;
			hint_enabled [current_question] = false;
			open_question ();
			draw_regular ();
			started = true;
			minute = seconds = 0;
			GLib.Timeout.Add (1000, new GLib.TimeoutHandler (update_status));
			open_question ();
		}

		void create_layout ()
		{

			layout_default = new Pango.Layout (da_a.PangoContext);
			layout_red = new Pango.Layout (da_a.PangoContext);
			layout_blue = new Pango.Layout (da_a.PangoContext);

			layout_default.Wrap = Pango.WrapMode.Word;
			layout_red.Wrap = Pango.WrapMode.Word;
			layout_blue.Wrap = Pango.WrapMode.Word;

			layout_default.FontDescription = Pango.FontDescription.FromString ("Ahafoni CLM Bold 12");
			layout_red.FontDescription = Pango.FontDescription.FromString ("Ahafoni CLM Bold 12");
			layout_blue.FontDescription = Pango.FontDescription.FromString ("Ahafoni CLM Bold 12");
		}

		void create_markup (string text)
		{
			layout_default.SetMarkup (text);
			layout_red.SetMarkup ("<span background=" + (char)34 + "red" + (char)34 + ">" + text + "</span>");
			layout_blue.SetMarkup ("<span background=" + (char)34 + "blue" + (char)34 + ">" + text + "</span>");
		}

		int get_score ()
		{
			wrong = right = score = 0;
			for (int i = 0; i < questions.count (); i++) {
				if (questions.get_question (i).is_rigth_answer (user_choices [i])) {
					score++;
					right++;
				} else
					wrong++;
			}
			return score;
		}

		void open_question ()
		{
			CauHoi this_question = questions.get_question (current_question);

			answer_a = this_question.answer_a;
			answer_b = this_question.answer_b;
			answer_c = this_question.answer_c;
			answer_d = this_question.answer_d;

//			rbtn_a.Label = this_question.answer_a;
//			rbtn_b.Label = this_question.answer_b;
//			rbtn_c.Label = this_question.answer_c;
//			rbtn_d.Label = this_question.answer_d;
			tv_question.Buffer.Text = this_question.question;

			string user_choice = user_choices [current_question];
			if (user_choice == "a")
				rbtn_a.Active = true;
			else if (user_choice == "b")
				rbtn_b.Active = true;
			else if (user_choice == "c")
				rbtn_c.Active = true;
			else
				rbtn_d.Active = true;
		}

		void save_choice ()
		{
			string it;
			if (rbtn_a.Active == true)
				it = "a";
			else if (rbtn_b.Active == true)
				it = "b";
			else if (rbtn_c.Active == true)
				it = "c";
			else
				it = "d";

			user_choices [current_question] = it;
		}

		bool update_status ()
		{
			if (!started) {
				lb_remain_time.Text = "";
				return false;
			} else 
			if (++seconds == 60) {
				++minute;
				seconds = 0;
			}

			lb_remain_time.Text = minute + " : " + seconds;
			return true;
		}

		protected void OnStop (object sender, EventArgs e)
		{
			minute = seconds = 0;
			started = false;
			GLib.Timeout.Add (0, new GLib.TimeoutHandler (update_status));
			get_score ();
			fast = new fas (wrong, right, score);
			btn_start.Sensitive = btn_stop.Sensitive = btn_hint.Sensitive = false;
		}

		protected void OnPrevious (object sender, EventArgs e)
		{
			save_choice ();
			if (current_question > 0) {

				current_question--;
				open_question ();
				if (hint_enabled [current_question] == true)
					draw_hinted();
				else
				{
					hint_enabled [current_question] = false;
					draw_regular();
				}

			}
		}

		protected void OnNext (object sender, EventArgs e)
		{
			save_choice ();



			if (++current_question < questions.count ()) {

				open_question ();
				if (hint_enabled [current_question] == true)
					draw_hinted();
				else
				{
					hint_enabled [current_question] = false;
					draw_regular();
				}

			} else 
				current_question = questions.count () - 1;
		}

		protected void OnHint (object sender, EventArgs e)
		{ 
			if (!hint_enabled [current_question]) {
				draw_hinted ();
				hint_enabled [current_question] = true;
			} else {
				draw_regular ();
				hint_enabled [current_question] = false;
			}

		}

		void draw_regular ()
		{
			draw_white ();
			create_markup (answer_a);
			da_a.GdkWindow.DrawLayout (da_a.Style.TextGC (StateType.Normal), 0, 0, layout_default);
			create_markup (answer_b);
			da_b.GdkWindow.DrawLayout (da_b.Style.TextGC (StateType.Normal), 0, 0, layout_default);
			create_markup (answer_c);
			da_c.GdkWindow.DrawLayout (da_c.Style.TextGC (StateType.Normal), 0, 0, layout_default);
			create_markup (answer_d);
			da_d.GdkWindow.DrawLayout (da_d.Style.TextGC (StateType.Normal), 0, 0, layout_default);
		}

		void draw_white ()
		{
			da_a.GdkWindow.DrawRectangle(da_a.Style.WhiteGC, true, 0, 0, da_a.WidthRequest, da_a.HeightRequest);
			da_b.GdkWindow.DrawRectangle(da_a.Style.WhiteGC, true, 0, 0, da_b.WidthRequest, da_b.HeightRequest);
			da_c.GdkWindow.DrawRectangle(da_a.Style.WhiteGC, true, 0, 0, da_c.WidthRequest, da_c.HeightRequest);
			da_d.GdkWindow.DrawRectangle(da_a.Style.WhiteGC, true, 0, 0, da_d.WidthRequest, da_d.HeightRequest);
		}

		void draw_hinted ()
		{
			draw_white ();
			if (user_choices [current_question] == "a") {
				create_markup (answer_a);
				da_a.GdkWindow.DrawLayout (da_a.Style.TextGC (StateType.Normal), 0, 0, layout_red);
			} else if (user_choices [current_question] == "b") {
				create_markup (answer_b);
				da_b.GdkWindow.DrawLayout (da_b.Style.TextGC (StateType.Normal), 0, 0, layout_red);
			} else if (user_choices [current_question] == "c") {
				create_markup (answer_c);
				da_c.GdkWindow.DrawLayout (da_c.Style.TextGC (StateType.Normal), 0, 0, layout_red);
			} else {
				create_markup (answer_d);
				da_d.GdkWindow.DrawLayout (da_d.Style.TextGC (StateType.Normal), 0, 0, layout_red);
			}

			if (questions.get_question (current_question).rigth_answer == "a") {
				create_markup (answer_a);
				da_a.GdkWindow.DrawLayout (da_a.Style.TextGC (StateType.Normal), 0, 0, layout_blue);
			} else if (questions.get_question (current_question).rigth_answer == "b") {
				create_markup (answer_b);
				da_b.GdkWindow.DrawLayout (da_b.Style.TextGC (StateType.Normal), 0, 0, layout_blue);
			} else if (questions.get_question (current_question).rigth_answer == "c") {
				create_markup (answer_c);
				da_c.GdkWindow.DrawLayout (da_c.Style.TextGC (StateType.Normal), 0, 0, layout_blue);
			} else {
				create_markup (answer_d);
				da_d.GdkWindow.DrawLayout (da_d.Style.TextGC (StateType.Normal), 0, 0, layout_red);
			}
		}

	}
}

